
package com.ebraga.trackerclub.android.util;

import java.util.Locale;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;


public class AndroidApplication extends Application {

    public static String DEFAULT_LANGUAGE = "en";

	private static final String TAG = DLog.logTag(AndroidApplication.class);

	private static Context instance;
	
	private Object gpsInfoVO;
	
	private Object userVO;
	
	private int notificationId = 0;

	// TODO: Use shared preferences instead?
	/**
	 * Session of user (also contains domain)
	 */
//	private static User currentUser;

	/**
	 * Language (-> date format, ...) used by app - chosen by user in settings
	 */
	private static Locale currentLocale;
	private static Locale systemLocale;

	/**
	 * Language (-> date format, ...) used by app - chosen by user in settings
	 */
    private static String currentLanguage = DEFAULT_LANGUAGE;

	/**
	 * Amount of hours to keep the user logged in after he entered his password
	 */
	private static int sessionTimeLimit = 16; // hours

	@Override
	public void onCreate() {
		super.onCreate();
		DLog.d(TAG, "onCreate");

		instance = this;

		// TODO: set locale to 'pt_BR' instead of 'new Locale("pb")' and match it to 'pb' when querying db
		// TODO: test locale switch without 'Locale.setDefault(locale)'

		// Remember what is was before we changed it
		AndroidApplication.systemLocale = Locale.getDefault();
		DLog.d(TAG, "Language before change: " + getBaseContext().getResources().getConfiguration().locale.getLanguage());
        DLog.d(TAG, "Locale before change  : " + getBaseContext().getResources().getConfiguration().locale);

		// Very first time app is run: set default preferences, afterwards do nothing
//		PreferenceManager.setDefaultValues(this, SettingsFragment.SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE, SettingsFragment.RESOURCE, false);
//		At every startup, set language from preferences
//		SharedPreferences settings = getSharedPreferences(SettingsFragment.SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);


	}


	/**
	 * Whether or not the user has been logged in for the maximum allowed time
	 * @return
	 */
//	public static boolean sessionTimeLimitReached() {
//		return (new Date().getTime() - currentUser.getPreviousLogin()) / 1000 / 60 / 60 > sessionTimeLimit;
//	}

	/**
	 * Are we running in the emulator? Is the DEBUG flag set? 
	 * 
	 * @return boolean
	 */
	public static boolean inDebugMode() {
		return ("google_sdk".equals(Build.PRODUCT)) || ("sdk".equals(Build.PRODUCT));
	}
	
	public static Context getContext() {
		return instance;
	}
	
	/**
	 * Current user session
	 * Should never return null, apart from on the login screen
	 * 
	 * @return
	 */
//	public static User getCurrentUser() {
//		if (null == currentUser) {
//			currentUser = UserDAO.getInstance().getLoggedInUser();
//		}
////		if (null == currentUser || !currentUser.isLoggedIn()) {
////			// Show login screen
////			Intent intent = new Intent(context, LoginActivity.class);
////			context.startActivity(intent);
////			// Broadcast logout action to finish running BaseActivities
////			Intent broadcastIntent = new Intent();
////			broadcastIntent.setAction(BaseActivity.ACTION_LOGOUT);
////			context.sendBroadcast(broadcastIntent);
////		}
//		return currentUser;
//	}

	public static int getSessionTimeLimit() {
		return sessionTimeLimit;
	}
//	public static void setSessionTimeLimit(int sessionTimeLimit) {
//		MobileITSMApplication.sessionTimeLimit = sessionTimeLimit;
//	}

	public static String getCurrentLanguage() {
		return currentLanguage;
	}
	public static void setCurrentLanguage(String currentLanguage) {
    	DLog.d(TAG, "setCurrentLanguage: " + currentLanguage);
		AndroidApplication.currentLanguage = currentLanguage;
		// Current locale is always based on current language
		AndroidApplication.currentLocale = new Locale(currentLanguage);
	}
	public static Locale getCurrentLocale() {
		// Current locale is always based on current language
		if (null == currentLocale || !currentLocale.getLanguage().equals(getCurrentLanguage())) {
			currentLocale = new Locale(getCurrentLanguage());
		}
		return currentLocale;
	}
	public static Locale getSystemLocale() {
		return systemLocale;
	}

	/**
	 * Apply the user defined locale to the activity. 
	 * Needs to be called before super.onCreate() in BaseActivity, LoginActivity, ...
	 * 
	 * @param activity
	 */
	public static void applyCurrentLocale(Activity activity) {
		Resources resources = activity.getBaseContext().getResources();
        Configuration config = resources.getConfiguration();
	    Locale.setDefault(AndroidApplication.getCurrentLocale());
	    config.locale = AndroidApplication.getCurrentLocale();
	    resources.updateConfiguration(config, resources.getDisplayMetrics());
	}


	public Object getGpsInfoVO() {
		return gpsInfoVO;
	}


	public void setGpsInfoVO(Object gpsInfoVO) {
		this.gpsInfoVO = gpsInfoVO;
	}


	public int getNotificationId() {
		return notificationId;
	}


	public void setNotificationId(int notificationId) {
		this.notificationId = notificationId;
	}


	public Object getUserVO() {
		return userVO;
	}


	public void setUserVO(Object userVO) {
		this.userVO = userVO;
	}

}
